﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TruyenCuoiViet.ViewModels;

namespace TruyenCuoiViet.Source.Helper
{
    public class CrawlerHelper
    {
        ///Ajax/AjaxListbyCat.aspx?CatID=170&PageIndex=2
        public static async Task<HtmlDocument> GetDocument(string url, int page)
        {
            try
            {
                string adress;
                if (url.IndexOf('?') > 0)
                {
                    adress = string.Format("{0}{1}&pagenumber={2}", MainViewModel.HomeAdress, url, page);
                }
                else
                {
                    adress = string.Format("{0}{1}?pagenumber={2}", MainViewModel.HomeAdress, url, page);
                }
                HtmlDocument d = new HtmlDocument();
                HttpWebRequest request = HttpWebRequest.Create(adress) as HttpWebRequest;
                request.Method = "GET";
                //User-Agent	Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch)
                request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch)";
                request.CookieContainer = new CookieContainer();
                HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
                StreamReader reader = new StreamReader(response.GetResponseStream());
                String html = reader.ReadToEnd();
                response.Close();
                d.LoadHtml(html);
                return d;
            }
            catch
            {
                return null;
            }
        }
        public static async Task<HtmlDocument> GetDocument(string adress)
        {
            try
            {
                HtmlDocument d = new HtmlDocument();
                HttpWebRequest request = HttpWebRequest.Create(adress) as HttpWebRequest;
                request.Method = "GET";
                request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch)";
                request.CookieContainer = new CookieContainer();
                HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
                StreamReader reader = new StreamReader(response.GetResponseStream());
                String html = reader.ReadToEnd();
                response.Close();
                d.LoadHtml(html);
                return d;
            }
            catch
            {
                return null;
            }
        }
    }
}
